const app = getApp();

Page({
	data: {
    aid: '',
    name: '',
    phone: '',
    address: '',
    provinceName: '',
    cityName: '',
    areaName: '',
		StatusBar: app.globalData.StatusBar,
		CustomBar: app.globalData.CustomBar,
		hidden: true,
		region: [],
	},
	onLoad: function (option) {
	    wx.getSetting({
	        success: res => {
		        if (!res.authSetting['scope.userInfo']) {
		            wx.redirectTo({
		              	url: '/pages/auth/auth'
		            })
		        }
	        }
      });
      this.AddressInfo(option.aid)
	},
	RegionChange: function (e) {
		this.setData({
			region: e.detail.value
		})
  },
  AddressInfo:function(aid){
    const _this = this
    wx.request({
      method:"GET",
      url: 'http://localhost:8989/address/' + aid,
      success:(res) => {
        let region = []
        region[0] = res.data.data.provinceName
        region[1] = res.data.data.cityName
        region[2] = res.data.data.areaName
        console.log(res.data.data)
        _this.setData({
          aid: res.data.data.aid,
          name: res.data.data.name,
          phone: res.data.data.phone,
          address: res.data.data.address,
          region: region,
        })
      }
    })
  },
  Update:function(){
    const _this = this
    wx.request({
      method:"POST",
      url: 'http://localhost:8989/address',
      data: {
        aid: _this.data.aid,
        name: _this.data.name,
        phone: _this.data.phone,
        address: _this.data.address,
        provinceName: _this.data.region[0],
        cityName: _this.data.region[1],
        areaName: _this.data.region[2]
      },
      success:(res)=>{
        wx.navigateTo({
          url: '/pages/user/address/index',
        })
      }
    })
  }
	
});
